/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer.formatter;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.heresylabs.rostercomposer.roster.RosterEntry;
import org.heresylabs.rostercomposer.units.Category;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class SimpleFormatter extends AbstractFormatter {

    private final Set<Category> printedCategories = new HashSet<Category>();

    public SimpleFormatter(List<RosterEntry> roster) {
        super(roster);
        Collections.sort(roster);
    }

    @Override
    protected void printEntry(RosterEntry entry) {
        printCategory(entry);

        writer.print(entry.getUnit().getName());
        writer.print(' ');
        writer.print(entry.getCost());
        writer.println();
        if (entry.getNumber() > 1) {
            writer.print(entry.getNumber());
            writer.print(" models");
            writer.println();
        }

        List<OptionDescription> descriptions = entryOptions(entry);
        if (descriptions != null && descriptions.size() > 0) {
            int max = descriptions.size() - 1;
            for (int i = 0; i < descriptions.size(); i++) {
                OptionDescription desc = descriptions.get(i);
                int n = desc.getCount();
                if (n > 1) {
                    writer.print(n);
                    writer.print('*');
                }
                writer.print(desc.getName());
                if (i != max) {
                    writer.print("; ");
                }
            }
            writer.println();
        }

        writer.println();
    }

    private void printCategory(RosterEntry entry) {
        Category category = entry.getCategory();
        if (printedCategories.contains(category)) {
            return;
        }
        printedCategories.add(category);
        writer.println(category.getName());
        writer.println();
    }

}
